<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Aggregation matcher
 * 
 * Entity resolution matcher setup to match aggregated resources in an aggregation.
 */
class escape_entityresolution_AggregationMatcher extends entityresolution_Matcher
{
	/**
	 * Repository
	 * @var escape_repository_Repository
	 */
	protected $repository;
	
	/**
	 * Aggregation
	 * @var escape_repository_OreAggregation
	 */
	protected $aggregation;
	
	/**
	 * Create an aggregation block source instance.
	 * @param escape_repository_Repository $repository
	 * @param escape_repository_OreAggregation $aggregation
	 */
	public function __construct($repository, $aggregation)
	{
		$this->repository = $repository;
		$this->aggregation = $aggregation;
		
		// data filters used on query entities
		$queryDataFilters = array(
			new entityresolution_datafilter_PersonNameExpander($this->repository->repositorySchema),
			new entityresolution_datafilter_SubTypeExpander($this->repository->repositorySchema),
			new entityresolution_datafilter_SubPropertyExpander($this->repository->repositorySchema),
			new escape_entityresolution_datafilter_ExternalUriExpander()
		);
		
		// data filters used on block entities
		$blockDataFilters = array(
			new entityresolution_datafilter_PersonNameExpander($this->repository->repositorySchema),
			new entityresolution_datafilter_SubTypeExpander($this->repository->repositorySchema),
			new escape_entityresolution_datafilter_InternalRelationFilter()
		);
		
		// setup the data filter chains
		$queryDataFilterChain = new entityresolution_DataFilterChain($queryDataFilters);
		$blockDataFilterChain = new entityresolution_DataFilterChain($blockDataFilters);
		
		// setup the entity factories
		$queryEntityFactory = new entityresolution_entityfactory_Cached($queryDataFilterChain);
		$blockEntityFactory = new entityresolution_entityfactory_Cached($blockDataFilterChain);
		
		// setup the data sources
		$dataSources = array(
			new escape_entityresolution_datasource_Ifp($repository, $aggregation),
			new escape_entityresolution_datasource_SubType($repository, $aggregation)
		); 
		
		// setup the comparator predicate weights
		// increase weights for all title properties
		$predicateWeights = array();
		$schemaRdfResources = $repository->repositorySchema->getResources();
		foreach($schemaRdfResources as $schemaRdfResource)
		{
			/* @var $schemaRdfResource simplerdf_RdfResource */
			
			$titleProperties = $schemaRdfResource->getObjects('http://purl.utwente.nl/ns/escape-system.owl#hasTitleProperty');
			foreach($titleProperties as $titleProperty)
			{
				$predicateWeights[$titleProperty] = 2.0;
			}
		}
		
		// setup the comparator
		$comparator = new entityresolution_comparator_Levenshtein($predicateWeights);
		
		// setup the matcher
		parent::__construct($dataSources, $queryEntityFactory, $blockEntityFactory, $comparator);
	}
}